package geektime.spring.springbucks;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import geektime.spring.springbucks.utils.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
public class SpringBucksApplication implements ApplicationRunner {
	@Autowired
	CoffeeService coffeeService;
	@Autowired
	CacheService cacheService;
	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		Coffee coffee = new Coffee();
		coffee.setId(1000L);
		coffee.setName("test");
		coffee.setPrice(Money.of(CurrencyUnit.of("CNY"), 20.0));
		coffeeService.createCoffee(coffee);
		log.info("this is createCoffee suceessful!");
		coffee.setId(1000L);
		coffee.setName("test001");
		coffee.setPrice(Money.of(CurrencyUnit.of("CNY"), 80.0));
		coffeeService.updateCoffeeById(coffee);
		log.info("this is updateCoffeeById suceessful!");
		log.info("this is findCoffeeById:{}",coffeeService.findCoffeeById(1000L));
		coffeeService.deleteCoffeeById(1000L);
		log.info("this is deleteCoffeeById suceessful!");
		List<Coffee> lstCoffee = coffeeService.findAll();
		log.info("this is findAll:{}",lstCoffee);
		cacheService.add("AllCoffee",lstCoffee);
		log.info("this is CacheCoffee:{}",cacheService.get("AllCoffee"));
		log.info("this is findAllWithParam:{}",coffeeService.findAllWithParam(2,3));
	}
}

